<link rel="stylesheet" type="text/css" href="../core/resource/css/coupon.css">
<div ng-include="sys_html('common/_header')"></div>
<div class='fui-page fui-page-current'>
	<div class="jy2-headertopbg">
			<div class="fui-header" style="background:none">
				<div class="fui-header-left">
					<a class="back" style="color:#fff" href="javascript:" ng-click="goback()"></a>
				</div>
				<div class="title bold" style="font-size:1rem;color:#fff">领取优惠券</div>
				<div class="fui-header-right" style="padding-right:0.5rem;color:#fff">
						<i class="icon jinyunicon icon-liaotian" ng-click="access_json('jy_weishop/member.index/')" style="font-size: 1rem;"></i>
				</div>
			</div>
	</div>
	<div class='fui-content coupon-index-bg jy2-headertop'>
		<div class="fui-tab-scroll">
				<div ng-class="{'item':1, 'on':pagedata.coupontype ==0}" ng-click="access_json('jy_weishop/coupon.index/main/coupontype=0')">全部优惠券</div>
				<div ng-class="{'item':1, 'on':pagedata.coupontype ==1}" ng-click="access_json('jy_weishop/coupon.index/main/coupontype=1')">购物优惠券</div>
				<div ng-class="{'item':1, 'on':pagedata.coupontype ==2}" ng-click="access_json('jy_weishop/coupon.index/main/coupontype=2')">充值优惠券</div>	
				<div ng-class="{'item':1, 'on':pagedata.coupontype ==10}" ng-click="access_json('jy_weishop/coupon.index/main/coupontype=10')">微信卡券</div>					
		</div>
		<div id="carousel-container" class="carousel slide" data-role="carousel" data-param-auto="true" ng-if="pagedata.couponset.hdps">
			<div class="carousel-inner" role="listbox">
				<div ng-repeat="nav in pagedata.couponset.hdps" ng-class="{'item':1, 'active':$index==0}" >
					<a href="{{nav.url}}">
						<img ng-src="{{tomedia(nav.image) || '../core/resource/images/banner-1.jpg'}}" style="display: block; width: 100%; height: 100%;">
					</a>
				</div>
			</div>
			<style>
					.fui-swipe-page .fui-swipe-bullet {background: {background}; opacity: {opacity};}
					.fui-swipe-page .fui-swipe-bullet.active {opacity: 1;}
			</style>
			<div id="carousel-jinyun" class="fui-swipe-page" style="padding: 0 5px; bottom: 10px; ">
				<div class="fui-swipe-bullet" ng-repeat="nav in pagedata.couponset.hdps" ng-class="{'active':$index==0}" data-target="#carousel-container" data-slide-to="{{$index}}"></div>
			</div>
		</div>
		<script>
			require(['bootstrap', 'hammer'], function($, Hammer){
				$('#carousel-container').carousel();
				var mc = new Hammer($('#carousel-container').get(0));
				mc.on("panleft", function(ev) {
					$('#carousel-container').carousel('next');
				});
				mc.on("panright", function(ev) {
					$('#carousel-container').carousel('prev');
				});
			});
		</script>
		<div ng-if="pagedata.list.length<=0" class="fui-message fui-message-popup in content-empty" style=" margin-top: 0; padding-top: 0; position: relative; height: auto; background: none;">
				<div class="icon ">
					<i class="icon icon-information"></i>
				</div>
				<div class="content">还没有发布优惠券~</div>
		</div>
		<!--内容加载-->
		<div id='container' class="coupon-container coupon-index-list">				
			<a ng-repeat="coupon in pagedata.list" ng-if="coupon.status == 1"  class="coupon-item {{coupon.color}}" style="height:5.6rem">
				<div ng-if="coupon.settitlecolor ==1" class="coupon-type"   style="background:{{coupon.titlecolor}">{{coupon.tagtitle}}</div>
				<div ng-if="coupon.settitlecolor !=1" class="coupon-type"   >{{coupon.tagtitle}}</div>
				<div class="coupon-left" ng-click="access_json('jy_weishop/coupon.detail//id='+coupon.id)">
					<div class="title">{{coupon.title3}}</div>
					<div class="subtitle">{{coupon.title2}}</div>
					<div ng-if="coupon.t>0 && coupon.issend ==1" class="subtitle light">剩余{{coupon.last}}/{{coupon.t}}张</div>
					<div ng-if="coupon.t<=0 && coupon.issend ==1" class="subtitle light">不限制</div>
				</div>
				<div class="coupon-right">
					<div class="title">{{coupon.title}}</div>
					<div class="subtitle">{{coupon.title5}}</div>
					<div ng-if="coupon.merchname" class="subtitle">限购[{{coupon.merchname}}]店铺商品</div>
					<div ng-if="coupon.issend == 0 && coupon.issendtime ==1" class="subtitle light">
						<span>距离开始：</span>
						<span id="start{{coupon.id}}" class="starttime">
                             <span class="day">-</span>
                             <span class="dw">:</span>
                             <span class="hour">-</span>
                             <span class="dw">:</span>
                             <span class="minute">-</span>
                             <span class="dw">:</span>
                             <span class="second">-</span>
                        </span>
					</div>
					<div ng-if="coupon.issend == 1 && coupon.issendtime ==1" class="subtitle light">
						<span>距离结束：</span>
						<span id="end{{coupon.id}}" class="starttime">
                             <span class="day">-</span>
                             <span class="dw">:</span>
                             <span class="hour">-</span>
                             <span class="dw">:</span>
                             <span class="minute">-</span>
                             <span class="dw">:</span>
                             <span class="second">-</span>
                        </span>
					</div>
					<div ng-if="coupon.issend == 2 && coupon.issendtime ==1" class="subtitle light">结束时间：{{coupon.sendtime.tend}}</div>
					<div class="usetime">
						<div class="text">{{coupon.title4}}</div>
						<div ng-if="coupon.isdisa ==1 && coupon.issend ==1" class="usebtn disabled">已发完</div>
						<div ng-if="coupon.issend ==2" class="usebtn disabled">已结束</div>
						<div ng-if="coupon.isdisa !=1 && coupon.issend ==1" class="usebtn" href="javascript:;" ng-click="access_json('jy_weishop/coupon.detail/main/id='+coupon.id)">立即领取</div>
						<div ng-if="coupon.issend ==0" class="usebtn" ng-click="post({id:coupon.id},'jy_weishop/coupon.index/tx/')">提醒我</div>
						<div ng-if="coupon.coupontype ==10" class="usebtn" ng-click="pagedata.addwxcard(coupon.card_id,coupon.cardExt)">立即领取</div>
					</div>
				</div>
			</a>
		
		</div>
	</div>
</div>
<div ng-include="sys_html('common/pluginnav',true)"></div>
<script>
setTimeout('uptime()',1000);
function uptime(){
	var ntime = scope.pagedata.ntime;
	var list = scope.pagedata.list
	for (var i=0;i<list.length;i++){
		if(list[i].issend==1 && list[i].issendtime ==1){
			endDown(list[i].sendtime.tend2, ntime, "#end"+list[i].id, "#end"+list[i].id+" .day", "#end"+list[i].id+" .hour", "#end"+list[i].id+" .minute", "#end"+list[i].id+" .second", "#end"+list[i].id+" .msec");
		}else if(list[i].issend==0 && list[i].issendtime ==1){
			startDown(list[i].sendtime.tstart2, ntime, "#start"+list[i].id, "#start"+list[i].id+" .day", "#start"+list[i].id+" .hour", "#start"+list[i].id+" .minute", "#start"+list[i].id+" .second", "#start"+list[i].id+" .msec");
		}
	}
}
scope.pagedata.addwxcard=function(card_id,cardExt){
	wx.addCard({
		cardList: [{
			cardId: card_id,
			cardExt: cardExt,
		}], // 需要添加的卡券列表
		success: function (res) {
			var cardList = res.cardList; // 添加的卡券列表信息
			scope.massage('领券成功！请前往微信卡包查看!');
		}
	});
}
</script>
<script>
// 结束倒计时
function endDown(etime, ntime, boxobj, day_elem, hour_elem, minute_elem, second_elem, msec_elem) {
	console.log('endDown');
	console.log(etime);console.log(ntime);
    var now_time = new Date(ntime * 1000);
    var end_time = new Date(etime * 1000);
	
    var native_time = new Date().getTime();     //本地时间
    var now_cha = now_time - native_time;       //服务器和本地时间差
    var native_end_time = end_time - now_cha;   //本地结束时间
    var sys_second = 0;
	console.log('end_time');console.log(end_time)
	console.log('now_cha');console.log(now_cha)
	console.log('native_end_time');console.log(native_end_time)
    endDowntimer = setInterval(function () {
        // 检查本地时间是否更改
        sys_second = (native_end_time - new Date().getTime()) / 100; //本地结束剩余时间
        sys_second -= 1;
        var day = Math.floor((sys_second / 36000) / 24);
        var hour = Math.floor((sys_second / 36000) % 24);
        var minute = Math.floor((sys_second / 600) % 60);
        var second = Math.floor((sys_second / 10) % 60);
        var msec = Math.floor(sys_second % 10); //毫秒

        day = day >= 0 ? day : 0;
        hour = hour >= 0 ? hour : 0;
        minute = minute >= 0 ? minute : 0;
        second = second >= 0 ? second : 0;
        msec = msec >= 0 ? msec : 0;
		//console.log('hour');console.log(hour)
        day_elem && $(day_elem).text(day);//计算天
        $(hour_elem).text(hour < 10 ? "0" + hour : hour);//计算小时
        $(minute_elem).text(minute < 10 ? "0" + minute : minute);//计算分
        $(second_elem).text(second < 10 ? "0" + second : second);// 计算秒
        $(msec_elem).text(msec);// 计算秒的1/10
        native_time = new Date().getTime();
    }, 100);
}
// 开始时间倒计时
function startDown(stime, ntime, boxobj, day_elem, hour_elem, minute_elem, second_elem, msec_elem) {
    var now_time = new Date(ntime * 1000);
    var end_time = new Date(stime * 1000);
    var native_time = new Date().getTime(); //本地时间
    var now_cha = now_time - native_time; //服务器和本地时间差
    var native_end_time = end_time - now_cha; //本地结束时间
    var sys_second = 0;
    startDowntimer = setInterval(function () {
        sys_second = (native_end_time - new Date().getTime()) / 100; //本地结束剩余时间
        if (sys_second > 0) {
            sys_second -= 1;
            var day = Math.floor((sys_second / 36000) / 24);
            var hour = Math.floor((sys_second / 36000) % 24);
            var minute = Math.floor((sys_second / 600) % 60);
            var second = Math.floor((sys_second / 10) % 60);
            var msec = Math.floor(sys_second % 10); //毫秒
            day_elem && $(day_elem).text(day);//计算天
            $(hour_elem).text(hour < 10 ? "0" + hour : hour);//计算小时
            $(minute_elem).text(minute < 10 ? "0" + minute : minute);//计算分
            $(second_elem).text(second < 10 ? "0" + second : second);// 计算秒
            $(msec_elem).text(msec);// 计算秒的1/10
            native_time = new Date().getTime();
        }else{
			clearInterval(startDowntimer);
		}
    }, 100);
}
</script>